// import _ from 'lodash';
// import urls from './apiUrls';
// import axios from 'axios';
// import qs from 'qs';

/**
 * [request Ajax 公共库]
 * @param  {[String]} apiName   [api 缩写名]
 * @param  {[Object]} paramsObj [Post 参数]
 */
let BW = {
  evil: (fn) => {
    var Fn = Function;  // 一个变量指向Function，防止有些前端编译工具报错
    return new Fn('return ' + fn)();
  },
  getRequest: function() {
    // 格式化url参数
    let url = location.search; // 获取url中"?"符后的字串
    let theRequest = {}
    if (url.indexOf('?') !== -1) {
      let str = url.substr(1)
      let strs = str.split('&')
      for (let i = 0; i < strs.length; i++) {
        theRequest[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1])
      }
    }
    return theRequest;
  },
  /*
  * url 目标url
  * arg 需要替换的参数名称
  * argVal 替换后的参数的值
  * return url 参数替换后的url
  */
  changeURLArg: (url, arg, argVal) => {
    var pattern = arg + '=([^&]*)';
    var replaceText = arg + '=' + argVal
    if (url.match(pattern)) {
      let tmp = '/(' + arg + '=)([^&]*)/gi'
      tmp = url.replace(BW.evil(tmp), replaceText)
      return tmp
    } else {
      if (url.match('[?]')) {
        return url + '&' + replaceText
      } else {
        return url + '?' + replaceText;
      }
    }
  },
  // 删除 url 中指定参数,并返回 url
  funcUrlDelL: nameLst => {
    let loca = window.location;
    let baseUrl = loca.origin + loca.pathname + '?';
    let hash = loca.hash;
    let query = loca.search.substr(1);
    let url = ''
    let obj = {}
    let arr = query.split('&');
    for (let i = 0; i < arr.length; i++) {
      arr[i] = arr[i].split('=');
      obj[arr[i][0]] = arr[i][1];
      for (let j = 0; j < nameLst.length; j++) {
        if (query.indexOf(nameLst[j]) > -1) {
          delete obj[nameLst[j]];
        };
      }
    }
    url = baseUrl + JSON.stringify(obj).replace(/["{}]/g, '').replace(/:/g, '=').replace(/,/g, '&') + hash;
    return url
  },
  dateFtt: (date, fmt) => { // 日期格式化
    var o = {
      'M+': date.getMonth() + 1,                 // 月份
      'd+': date.getDate(),                    // 日
      'h+': date.getHours(),                   // 小时
      'm+': date.getMinutes(),                 // 分
      's+': date.getSeconds(),                 // 秒
      'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
      'S': date.getMilliseconds()             // 毫秒
    };
    if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)); }
    for (var k in o) {
      if (new RegExp('(' + k + ')').test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length))); }
    }
    return fmt;
  }
}

BW.Toast = (() => {        //  展示toast提示框
  var timeout;
  return function(msg) {
    if (!msg) return
    clearTimeout(timeout);
    wVue.$store.commit('showToast', msg);
    timeout = setTimeout(function() {
      wVue.$store.commit('hideToast')
    }, 2000);
  };
})();

export default BW;
